﻿Ext.define("PSI.User.UserEditForm", {
    createUI: function (entity) {
        var wnd = Ext.create("Ext.window.Window", {
            title: entity == null ? "新增用户" : "编辑用户",
            modal: true,
            onEsc: Ext.emptyFn,
            width: 400,
            height: 220,
            layout: "fit",
            items: [
                {
                    id: "editForm",
                    xtype: "form",
                    layout: "form",
                    height: "100%",
                    bodyPadding: 5,
                    defaultType: 'textfield',
                    fieldDefaults: {
                        labelWidth: 60,
                        labelAlign: "right",
                        labelSeparator: "",
                        msgTarget: 'side'
                    },
                    items: [
                        {
                            xtype: "hidden",
                            name: "Id",
                            value: entity == null ? null : entity.id
                        },
                        {
                            id: "editLoginName",
                            fieldLabel: "登录名",
                            allowBlank: false,
                            blankText: "没有输入登录名",
                            beforeLabelTextTpl: PSI.Const.REQUIRED,
                            name: "LoginName",
                            value: entity == null ? null : entity.loginName
                        },
                        {
                            id: "editName",
                            fieldLabel: "姓名",
                            allowBlank: false,
                            blankText: "没有输入姓名",
                            beforeLabelTextTpl: PSI.Const.REQUIRED,
                            name: "Name",
                            value: entity == null ? null : entity.name
                        },
                        {
                            id: "editOrgCode",
                            fieldLabel: "编码",
                            allowBlank: false,
                            blankText: "没有输入编码",
                            beforeLabelTextTpl: PSI.Const.REQUIRED,
                            name: "OrgCode",
                            value: entity == null ? null : entity.orgCode
                        },
                        {
                            id: "editOrgName",
                            xtype: "psi_org_editor",
                            fieldLabel: "所属组织",
                            allowBlank: false,
                            blankText: "没有选择组织机构",
                            beforeLabelTextTpl: PSI.Const.REQUIRED,
                            parentItem: this,
                            value: entity == null ? null : entity.orgName
                        },
                        {
                            id: "editOrgId",
                            xtype: "hidden",
                            name: "OrgId",
                            value: entity == null ? null : entity.orgId
                        },
                        {
                            xtype: "radiogroup",
                            fieldLabel: "能否登录",
                            columns: 2,
                            items: [
                                {
                                    boxLabel: "允许登录", name: "Enabled", inputValue: true,
                                    checked: entity == null ? true : entity.enabled == 1
                                },
                                {
                                    boxLabel: "<span style='color:red'>禁止登录</span>",
                                    name: "Enabled", inputValue: false,
                                    checked: entity == null ? false : entity.enabled != 1
                                }
                            ]
                        }
                    ],
                    buttons: [
                        {
                            text: "确定",
                            formBind: true,
                            iconCls: "psi-button-ok",
                            handler: this.onOK,
                            scope: this
                        },
                        {
                            text: "取消", handler: function () {
                                var me = this;
                                PSI.MsgBox.confirm("请确认是否取消操作?", function () {
                                    me.wnd.close();
                                });
                            }, scope: this
                        }
                    ]
                }
            ]

        });

        this.wnd = wnd;
    },

    show: function (entity) {
        this.createUI(entity);

        this.wnd.show();
    },

    setOrg: function (data) {
        var editOrgName = Ext.getCmp("editOrgName");
        editOrgName.setValue(data.fullName);

        var editOrgId = Ext.getCmp("editOrgId");
        editOrgId.setValue(data.id);
    },

    // private
    onOK: function () {
        var f = Ext.getCmp("editForm");
        var el = f.getEl();
        el.mask("数据保存中...");
        f.submit({
            url: "/User/EditUser",
            method: "POST",
            success: function (form, action) {
                el.unmask();
                PSI.MsgBox.showInfo("数据保存成功", function () {
                    window.location.reload();
                });
            },
            failure: function (form, action) {
                el.unmask();
                PSI.MsgBox.showInfo(action.result.msg, function () {
                    Ext.getCmp("editName").focus();
                });
            }
        });
    }

});